package com.uenpay.im.invokeitems.chat;

import com.qiyunxin.android.http.net.HttpInvokeItem;
import com.uenpay.im.configuration.APIConfiguration;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;

/**
 *  修改群资料
 * @author SL
 * @copyRight http://www.qiyunxin.com
 */
public class UpdateGroupInfoInvokeItem extends HttpInvokeItem {
	public class UpdateGroupInfoInvokeItemResult {
		public int status;
		public String msg;
		public long timeStamp;
	}

	public UpdateGroupInfoInvokeItemResult getOutput() {
		return (UpdateGroupInfoInvokeItemResult) GetResultObject();
	}

	/**
	 * 修改群资料
	 * 
	 * @param chat_id
	 *            群id
	 * @param name
	 *            修改的名称
	 * @param type
	 *            1：群名称2我在群聊的名称3群简介
	 */
	public UpdateGroupInfoInvokeItem(String chat_id, String name, int type) {
		String url = "";
		if (type == 1) {
			url = APIConfiguration.getBaseUrl() + "Chat/Chat/setChatName?"
					+ APIConfiguration.getCustIdAndToken();
		} else if (type == 2) {
			url = APIConfiguration.getBaseUrl() + "Chat/Chat/setNickname?"
					+ APIConfiguration.getCustIdAndToken();
		} else if (type == 3) {
			url = APIConfiguration.getBaseUrl() + "Chat/Chat/setChatIntroduce?"
					+ APIConfiguration.getCustIdAndToken();
		}

		HashMap<String, String> mRequestParams = new HashMap<String, String>();
		mRequestParams.put("chatid", chat_id);
		if (type == 1) {
			mRequestParams.put("chatname", name);
		} else if (type == 2) {
			mRequestParams.put("nickname", name);
		} else if (type == 3) {
			mRequestParams.put("introduce", name);
		}
		SetRequestParams(mRequestParams);
		SetUrl(url);
	}

	@Override
	protected Object DeserializeResult(String responseBody) {
		UpdateGroupInfoInvokeItemResult result = new UpdateGroupInfoInvokeItemResult();
		try {
			JSONObject json = new JSONObject(responseBody);
			result.status = json.optInt("status");
			result.msg = json.optString("msg");
		} catch (JSONException e) {
			e.printStackTrace();
			return result;
		}

		return result;
	}

}
